﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TimeTracker.Entities
{
    public static class EntityExtensions
    {
        public static bool IsEntityId(this string value)
        {
            if (string.IsNullOrWhiteSpace(value))
                return false;

            var split = value.Split(new[] { '/' });
            if (split.Length != 2)
                return false;

            if (string.IsNullOrWhiteSpace(split[0]))
                return false;

            int result;
            if (!int.TryParse(split[1], out result))
                return false;

            return true;
        }

        public static bool IsEntityId(this string value, string entity)
        {
            if (string.IsNullOrWhiteSpace(value))
                return false;

            var split = value.Split(new[] { '/' });
            if (split.Length != 2)
                return false;

            if (split[0] != entity)
                return false;

            int result;
            if (!int.TryParse(split[1], out result))
                return false;

            return true;
        }

        public static int AsEntityIntId(this string value)
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException(string.Format("'{0}' is not an entity id.", value), "value");

            var split = value.Split(new[] { '/' });
            if (split.Length != 2)
                throw new ArgumentException(string.Format("'{0}' is not an entity id.", value), "value");

            if (string.IsNullOrWhiteSpace(split[0]))
                throw new ArgumentException(string.Format("'{0}' is not an entity id.", value), "value");

            int result;
            if (!int.TryParse(split[1], out result))
                throw new ArgumentException(string.Format("'{0}' is not an entity id.", value), "value");

            return result;
        }

        public static long AsEntityLongId(this string value)
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException(string.Format("'{0}' is not an entity id.", value), "value");

            var split = value.Split(new[] { '/' });
            if (split.Length != 2)
                throw new ArgumentException(string.Format("'{0}' is not an entity id.", value), "value");

            if (string.IsNullOrWhiteSpace(split[0]))
                throw new ArgumentException(string.Format("'{0}' is not an entity id.", value), "value");

            long result;
            if (!long.TryParse(split[1], out result))
                throw new ArgumentException(string.Format("'{0}' is not an entity id.", value), "value");

            return result;
        }

        public static string AsEntityStringId(this int value, string entityName)
        {
            return string.Format("{0}/{1}", entityName.ToLower(), value);
        }

        public static string GetNameFromEntityId(this string value)
        {
            if (!value.IsEntityId())
                throw new ArgumentException(string.Format("'{0}' is not an entity id.", value), "value");

            var split = value.Split(new[] { '/' });
            return split[0];
        }
    }
}
